package com.cssl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cssl.domain.Dtsorder;
import com.cssl.domain.Page;
import com.cssl.domain.dto.*;
import com.cssl.service.DtsorderService;
import com.cssl.util.InfoCode;
import com.cssl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Order")
@Api(value = "订单接口",tags = "订单相关操作")
public class DtsorderController {
    @Autowired
    DtsorderService dtsorderService;

    /**
     * 订单详情的list
     */
    @PostMapping("/list")
    Result dtsorderlist(@RequestBody DtsorderDto dtsorderDto){
        Map orderlist=dtsorderService.findPageorder(dtsorderDto);
        if (orderlist==null||ObjectUtil.isEmpty(orderlist.get("list"))) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,orderlist,System.currentTimeMillis());
    }

    @DeleteMapping("/delete")
    public Result deleteOrder(@RequestParam("ids") String ids) {
        boolean success = dtsorderService.orderdelete(ids); // 根据订单号删除
        if (success) {
            return Result.result(InfoCode.OK, "删除成功", null, System.currentTimeMillis());
        } else {
            return Result.result(InfoCode.FAILED, "删除失败", null, System.currentTimeMillis());
        }
    }

    @PostMapping("/addDirectUserOrder")
    @ApiOperation(value = "直购新增订单",notes = "团购,直购")
    public Result addDirectUserOrder(@RequestBody DirectUserOrder userOrder){
        return dtsorderService.addDirectUserOrder(userOrder);
    }

    @PostMapping("/addCarOrder")
    @ApiOperation(value = "购物车新增订单",notes = "购物车")
    public Result addCarOrder(@RequestBody Map<String, List<Integer>> map){
        List<Integer> add= map.get("goodsId");
        return dtsorderService.addCarOrder(add);
    }

//    @PostMapping("/initiateGroupPurchase")
//    @ApiOperation(value = "发起拼团")
//    public Result initiateGroupPurchase(@RequestBody GroupPurchaseDto groupPurchaseDto){
//        return dtsorderService.initiateGroupPurchase(groupPurchaseDto);
//    }

    @PostMapping("/getOrderInfo")
    @ApiOperation(value = "获得订单支付信息")
    public Result getOrderDetails(@RequestBody OrderInfoDto orderInfoDto){
       return dtsorderService.getOrderDetails(orderInfoDto);
    }


    @PostMapping("/commonAddOrder")
    @ApiOperation(value = "新增订单")
    public Result commonAddOrder(@RequestBody CommonOrderDto dto){
        return dtsorderService.commonAddOrder(dto);
    }

    @PostMapping("/updateorder")
    Result updatebrand(@RequestBody Map updateorder){
        int brandadd=dtsorderService.updateorder(updateorder);
        System.out.println("信息"+updateorder.size());
        return Result.result(InfoCode.OK,InfoCode.MESSAGE_OK,brandadd,System.currentTimeMillis());
    }

    @ApiOperation(value = "支付宝二维码支付")
    @PostMapping("/QRCode")
    public Result QRCode(@RequestBody Dtsorder dtsorder){
        return dtsorderService.QRCode(dtsorder);
    }
}
